package com.bw.dg.controller;

import com.bw.dg.pojo.Assetgl;
import com.bw.dg.pojo.People;
import com.bw.dg.service.AssetglService;
import com.bw.dg.util.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static java.lang.Double.isNaN;

/**
 * @ProjectName: dg
 * @Package: com.bw.dg.controller
 * @ClassName: AssetglController
 * @Author: 社会佩奇
 * @Description: AssetglCotroller
 * @Date: 2020/4/19 16:09
 * @Version: 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("/assetgl")
public class AssetglController {

    @Autowired
    private AssetglService assetglService;

    private final ResourceLoader resourceLoader;

    @Autowired
    public AssetglController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @RequestMapping("/findAssetglList")
    public List getAssetglList(){
        List list=assetglService.getAssetglList();
        return list;
    }

    @RequestMapping("/findAssetglByid")
    public Assetgl ChaAssetglByid(@RequestParam("id") String id){
        Assetgl assetgl=assetglService.ChaAssetglByid(id);
        return assetgl;
    }

    @ResponseBody
    @RequestMapping(value = "/addAssetgl",method = RequestMethod.POST)
    public String tianjiaAssetgl(String file,Assetgl assetgl,String data){
        assetgl.setPhoto(file);
        System.out.println(file);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse = simpleDateFormat.parse(data);
            assetgl.setData(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        assetgl.setId(UUID.randomUUID().toString());
        assetglService.tianjiaAssetgl(assetgl);
        return "添加成功";

    }

    @RequestMapping("findZhiban")
    public List getzhiban(){
        List list=assetglService.getzhiban();
        return list;
    }

    @RequestMapping("findBuMen")
    public List getBuMen(){
        List list=assetglService.getBuMen();
        return list;
    }

    @RequestMapping("findUser")
    public List getUser(){
        List list=assetglService.getUser();
        return list;
    }
    @RequestMapping("deleteAssetglById")
    public String delete(@RequestParam("id") String id){
        // 获取传回来的id字符串
        String[] ids= id.split(",");
        // 通过逗号分割字符串，获得所有的id，在mapper中通过mybatis提供的动态循环遍历并删除数组中对应id的值就行
        for (String aa:ids) {
            assetglService.shanchuAssetglById(aa);
        }
        return "删除成功";
    }


    @RequestMapping("/findAssetglListByID")
    public List getAssetglListByID(@RequestParam("id") String id){
        List assetgl=assetglService.getAssetglListByID(id);
        return assetgl;
    }

    @RequestMapping("/findAssetglListByZC_name")
    public List getAssetglListByIDZC_name(@RequestParam("zc_name") String zc_name){
        List assetgl=assetglService.getAssetglListByIDZC_name(zc_name);
        return assetgl;
    }



    @RequestMapping("exportList")
    public void exportList(HttpServletResponse response, String ids) {

        String fileName = "固定列表";

        List<Assetgl> users = assetglService.companyList(ids);
        // 列名
        String[] title = { "ID", "name", "添加方式","品牌","数量","价钱","模型","编号","理由","日期","状态","原因","地址","头型" };
        // map中的key
        String keys[] = {"id", "zc_name", "add_method", "brand", "zc_sum", "price",
                "models", "bh", "cost", "data", "zc_status", "yuanyin", "address", "photo"};
        try {
            Exce.start_download(response, fileName, users, title, keys);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("/geterweima")
    public String eRweima(@RequestParam("id") String id) {
        return assetglService.erweima(id);
    }

    @RequestMapping(value = "/showerweima")
    public ResponseEntity showerweima(String eRweima, HttpServletResponse response) throws IOException {
        eRweima = StringUtils.isNotEmpty(eRweima) ? ("file:"+ ErWeiMaUtil.uploadPath()+eRweima) : "uploadImg.jpg";
        System.out.println(eRweima);
        return ResponseEntity.ok(resourceLoader.getResource(eRweima));
    }

    @RequestMapping("baofeiAssetglById")
    public String xiugaibaofeiAssetglById(@RequestParam("id") String id){
        // 获取传回来的id字符串
        String[] ids= id.split(",");
        // 通过逗号分割字符串，获得所有的id，在mapper中通过mybatis提供的动态循环遍历并删除数组中对应id的值就行
        for (String aa:ids) {
            assetglService.xiugaibaofeiAssetglById(aa);
        }
        return "修改报废成功";
    }

    @RequestMapping("exportListAll")
    public void exportList2(HttpServletResponse response) {

        String fileName = "固定列表";

        List<Assetgl> users = assetglService.exportList2();
        // 列名
        String[] title = { "ID", "name", "添加方式","品牌","数量","价钱","模型","编号","理由","日期","状态","原因","地址","头型" };
        // map中的key
        String keys[] = {"id", "zc_name", "add_method", "brand", "zc_sum", "price",
                "models", "bh", "cost", "data", "zc_status", "yuanyin", "address", "photo"};
        try {
            Exce.start_download(response, fileName, users, title, keys);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
